﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

public partial class Master : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["uid"] == null)
        {
            Panel1.Visible = true;
            Panel2.Visible = false;
        }
        else
        {
            Panel1.Visible = false;
            Panel2.Visible = true;

        }

    }

    public void login(string struid, string strpwd)
    {
        if (ToolsDT.doLogin(struid, strpwd))
        {
            Session.Add("uid", struid);
            Session.Add("login", "ok");
            Panel1.Visible = false;
            Panel2.Visible = true;
            ContentPlaceHolder1.Visible = false;
        }
    }


    protected void btnlogin_Click(object sender, EventArgs e)
    {
        string struid = txtusername.Text.Trim();
        string strpwd = txtpassword.Text.Trim();
        login(struid, strpwd);
        Response.Redirect("~/GUI/HomeUser.aspx");
    }

    public void initCookies()
    {
        // if brower support cookies
        if (Request.Browser.Cookies)
        {
            if (Request.Cookies.Get("userInfo") != null)
            {
                // get cookies
                HttpCookie ck = Request.Cookies["userInfo"];
                string username = "";
                string password = "";
                username = ck["uid"];
                password = ck["pwd"];

                if (username != "" && password != "")
                {
                    login(username, password);
                }
            }
        }
        string log = (string)Session["login"];
        if (log == null || log == "")
        {
            Panel1.Visible = true;
            Panel2.Visible = false;
        }
        else
        {
            Panel2.Visible = true;
            Panel1.Visible = false;
        }

    }


    //Write cookies
    public void writeCookies(string uid, string pwd)
    {
        if (Request.Browser.Cookies)
        {
            if (Request.Cookies["userInfo"] == null)
            {
                //Create cookies save information user
                HttpCookie ckUserInfo = new HttpCookie("userInfo");
                ckUserInfo["uid"] = uid;
                ckUserInfo["pwd"] = pwd;
                ckUserInfo.Expires = DateTime.Now.AddDays(30);
                Response.Cookies.Add(ckUserInfo);
            }

        }
    }


}
